use Irssi 20020916.1556 ();
use strict;

use vars qw($VERSION %IRSSI);

# Hacked from thankop.pl to only show the person in a display, not to 
# publically thank them...


$VERSION="0.1";
%IRSSI = (
	authors => 'BC-bd, David \"Legooolas\" Gardner',
	contact => 'irssi@icmfp.com',
	name => 'showop',
	description => 'Remembers the last person who opped you on a channel',
	license => 'GPL v2',
	url => 'http://icmfp.com/irssi/',
);

#########
# USAGE
###
#
# Type '/showop' in a channel window to show the person who opped you
#
################
#
# Changelog (from thankop.pl)
#
# Version 0.1.2
#  - added version dependency, since some 0.8.4 users complained about a not working script
#
# Version 0.1.1
#  - unsetting of hash values is done with delete not unset.
#
# Version 0.1.0
#  - initial release
#
################

my %modes;
my %op;

sub cmd_showop {
	my ($data, $server, $witem) = @_;

#	Irssi::print("$witem->{name}");

	if (exists($op{$witem->{name}})) {
		my $by = $op{$witem->{name}};
		#$witem->command("/say $by: opthx");
		$witem->print("showop: You were opped by $by");
	} else {
		$witem->print("showop: *shrug*");
	}
}

sub mode_changed {
	my ($channel, $nick, $by) = @_;

#	Irssi::print("me: $channel->{server}->{nick}, $nick->{nick} by $by $channel->{name}: $channel->{chanop}");

	return if ($channel->{server}->{nick} ne $nick->{nick});
	
	if ($modes{$channel->{name}} == 0 && $channel->{chanop}) {
#		Irssi::print("$channel->{name} op by $by");
		$op{$channel->{name}} = $by;
	}

	$modes{$channel->{name}} = $channel->{chanop};

#	Irssi::print("me: $channel->{server}->{nick}, $nick->{nick} by $by $channel->{name}: $channel->{chanop}");
}

sub channel_joined {
	my ($channel) = @_;

	$modes{$channel->{name}} = $channel->{chanop};
#	Irssi::print("$channel->{name}: $channel->{chanop}");
}

sub channel_destroyed {
	my ($channel) = @_;

	delete($modes{$channel->{name}});
	delete($op{$channel->{name}});
}

sub init_modes {
	foreach my $channel (Irssi::channels()) {
		if ($channel->{chat_type} eq 'IRC') {
			$modes{$channel->{name}} = $channel->{chanop};
		}
#		Irssi::print("$channel->{name}: $channel->{chanop}");
	}
}

init_modes();

Irssi::command_bind('showop','cmd_showop');
Irssi::signal_add_last('nick mode changed', 'mode_changed');
Irssi::signal_add_last('channel joined', 'channel_joined');
Irssi::signal_add_last('channel destroyed', 'channel_destroyed');
